#include "util.h"

#include <pthread.h>
#include <threads.h>

namespace vlog::util
{

static thread_local std::string g_threadName;
static thread_local uint64_t g_threadID = 0;

uint64_t GetThreadID() noexcept
{
    if (g_threadID == 0)
    {
        g_threadID = ::syscall(SYS_gettid);
    }
    return g_threadID;
}

uint64_t GetTimeMs() noexcept
{

    time_t timeMs = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());

    return static_cast<uint64_t>(timeMs);
}

std::string GetThreadName() noexcept
{
    if (g_threadName.empty())
    {
        char threadName[16]{};
        ::pthread_getname_np(::pthread_self(), threadName, 16);
        g_threadName = threadName;
    }
    
    return g_threadName;
}
} // namespace vlog::util
